var app = getApp();
var formDatas = {};  
var utils = require("../../../utils/util.js");
Page({

  data: {
    userInfo:{},
    img_arr: [],
    picture_type:''
  },
  formSubmit: function (e) {
    var id = e.target.id
    formDatas.pictureType = e.detail.value.picture_type
   // formDatas.userInfo = this.data.userInfo
   // console.log(this.data.userInfo);
    formDatas.userName = this.data.userInfo.nickName
    formDatas.gender = this.data.userInfo.gender
    formDatas.language = this.data.userInfo.language
    formDatas.city = this.data.userInfo.city
    formDatas.province = this.data.userInfo.province
    formDatas.openid = this.data.userInfo.openid
    formDatas.session_key = this.data.userInfo.session_key
    formDatas.avatarUrl = this.data.userInfo.avatarUrl
    formDatas.country = this.data.userInfo.country
    this.upload()
  },

  upload: function () {
    
    var that = this
    for (var i = 0; i < this.data.img_arr.length; i++) {
      wx.uploadFile({
        url: 'http://127.0.0.1:8551/wx_upload_picture',
        filePath: that.data.img_arr[i],
        method: 'post',
        name: 'file',
        header: {
          "Content-Type":
          "multipart/form-data"
        },
        formData: formDatas,
        success: function (res) {
          console.log(res)
          if (res) {
            wx.showToast({
              title: '已提交发布！',
              duration: 3000
            });
          }
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  upimg: function () {
    var that = this;
    if (this.data.img_arr.length < 3) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },  
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.img_arr;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      img_arr: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.img_arr;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //console.log(userInfo)
      //更新数据
      that.setData({
        userInfo: userInfo

      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})